layui.use(['jquery','form'],function () {
    let form = layui.form;
    let $ = layui.$;
    //关闭按钮
    $('#close').click(function () {
        var layer = parent.layer;
        layer.close(layer.index)
    });
    setTimeout(initialValue,200);
    //初始赋值
    function initialValue() {
        $('#courseStatus').val($('#status').val());
        form.render();
    }
    //价格输入框验证
    form.verify({
        courseNprice: function (value, item) {
            if(value == ''){
                return '请输入价格';
            }
            var regPos = /^[0-9]+.?[0-9]*$/; //非负浮点数
            if(!regPos.test(value)){
                return '价格必须是数字';
            }
        }
    })
    //根据课程id修改课程
    form.on('submit(updateCourse)',function (obj) {
        var user = JSON.parse(localStorage.getItem('user'));
        $.ajax({
            url: '/course/updateCourseByCourseId',
            type: 'post',
            dataType: 'json',
            data: obj.field,
            success: function (data) {
                if(data.data){
                    //添加日志
                    $.ajax({
                        url: '/log/addLog',
                        type: 'post',
                        dataType: 'json',
                        data: {
                            tableName: 'js_course',
                            type: 4,
                            userId: user.data.userId,
                            remark: '修改课程',
                            logType: 2
                        },
                        success: function(data){
                            if(data.data.logId != null){
                                //如果修改了价格 就添加到历史价格详情中
                                if($('#coursePrice').val() != obj.field.courseNprice){
                                    $.ajax({
                                        url: '/course/getCourseByCourseId',
                                        dataType: 'json',
                                        data: {courseId: obj.field.courseId},
                                        success: function (data) {
                                            var course = data.data;
                                            $.ajax({
                                                url: '/courseJournal/addCourseHistoryDetails',
                                                type: 'post',
                                                dataType: 'json',
                                                data: {
                                                    courseId: obj.field.courseId,
                                                    courseName: course.courseName,
                                                    courseStatus: course.courseStatus,
                                                    courseCstatus: course.result,
                                                    userId: user.data.userId,
                                                    type: 2,
                                                    coursePrice: obj.field.coursePrice,
                                                    courseNprice: obj.field.courseNprice
                                                },

                                            });
                                        }
                                    });
                                }
                                var layer = parent.layer;
                                layer.close(layer.index)
                            }
                        }
                    });
                }
            }
        });
        return false;
    });
})